//=============================================================================
// RPG Maker MZ - Alternative Menu Screen
//=============================================================================

/*:
 * @target MZ
 * @plugindesc Alternative menu screen layout.
 * @author Yoji Ojima
 *
 * @help AltMenuScreen.js
 *
 * This plugin changes the layout of the menu screen.
 * It puts the commands on the top and the status on the bottom.
 *
 * It does not provide plugin commands.
 */

/*:ja
 * @target MZ
 * @plugindesc メニュー画面のレイアウトを変更します。
 * @author Yoji Ojima
 *
 * @help AltMenuScreen.js
 *
 * このプラグインは、メニュー画面のレイアウトを変更します。
 * コマンドを上側に、ステータスを下側に配置します。
 *
 * プラグインコマンドはありません。
 */
/* @ Tāgetto MZ* @ plugindesc menyū gamen no reiauto o henkō shimasu. * @ Author Yoji Ojima* * @ herupu AltMenuScreen. Js* * kono Puraguin wa, menyū gamen no reiauto o henkō shimasu. * Komando o uegawa ni, sutētasu o kasoku ni haichi shimasu. * * Puraguinkomando wa arimasen.
展开
185/5000
* @目标MZ
  * @plugindesc更改菜单屏幕的布局。
  * @作者小岛洋二
  *
  * @帮助AltMenuScreen.js
  *
  *此插件可更改菜单屏幕的布局。
  将*命令放在顶部，将状态放在底部。
  *
  *没有插件命令。
  */
(() => {
    
    //-----------------------------------------------------------------------------
    // 乘降窗口方法
    //

    function Scene_UpDown() {
        this.initialize(...arguments);
    }

    Scene_UpDown.prototype = Object.create(Scene_MenuBase.prototype);
    Scene_UpDown.prototype.constructor = Scene_UpDown;

    Scene_UpDown.prototype.initialize = function() {
        Scene_MenuBase.prototype.initialize.call(this);
    };

    Scene_UpDown.prototype.create = function() {
        Scene_MenuBase.prototype.create.call(this);
        this.createCommandWindow();
    };

    Scene_UpDown.prototype.stop = function() {
        Scene_MenuBase.prototype.stop.call(this);
        this._commandWindow.close();
    };

    Scene_UpDown.prototype.createBackground = function() {
        Scene_MenuBase.prototype.createBackground.call(this);
        this.setBackgroundOpacity(128);
    };

    Scene_UpDown.prototype.createCommandWindow = function() {
        const rect = this.commandWindowRect();
        this._commandWindow = new Window_UpDown(rect);
        this._commandWindow.setHandler("updownPart", this.upDownList.bind(this));
        this._commandWindow.setHandler("tow", this.towTank.bind(this));
        this._commandWindow.setHandler("cancel", this.popScene.bind(this));
        this.addWindow(this._commandWindow);
    };
    //乘降角色
    Scene_UpDown.prototype.upDownList = function(){
        //console.log($gameParty,$gameTanks,$gamePlayer,$gameMap,$gameActors,$gameTanks,this)
        const party = $gameParty._actors.length
        const index = this._commandWindow._index
        this.popScene();
        if(!index){
        //全体
            // let i = 0
            // allUpDown(i,party)
            for(let i = 0; i < party;i++){
                this.upDownActor(i)
            }
        }else{
            //角色
            this.upDownActor(index - 1)
        }
      
    }
    //牵引
    Scene_UpDown.prototype.towTank = function(){
        if($gameActors.checkDriver()){

            // 获取角色的坐标
            let first = $gamePlayer
            let p_x = first._realX
            let p_y = first._realY
            tanks = $gameMap._tanks
            
            if(!first._followers.isTow()){
                // 匹配当前大地图的战车 
                for(let i in tanks){
                    if(!tanks[i]){
                        continue
                    }
                    //console.log(`人类ID：${ac._actorId}`, `坦克ID：${tanks[i]._tankId}`)
                    if(`${p_x},${p_y}` == `${tanks[i]._realX},${tanks[i]._realY}`){
                        let tank = new Game_Follower($gamePlayer._followers._data.length + 1)
                        tank._characterName = tanks[i]._characterName;
                        tank._tankId = tanks[i]._tankId
                        tank.x = tanks[i]._x
                        tank.y = tanks[i]._y
                        console.log(tank)
                        $gamePlayer._followers._data.push(tank)
                        $gamePlayer.refresh()
                        let gametank = $gameTanks.tank(tanks[i]._tankId)
                        gametank._driver = -1
                        gametank.refresh()
                        AudioManager.playSe({name:'0x18-Shot_1',pitch:100,volume:100,pan:0});
                        $gameMessage._texts.push( `${tanks[i]._name} 被牵引...`)
                        $gameMap.removeTank(tanks[i]._tankId)
                        this.popScene();
                        return
                    }
                }
            }else{
                first._followers.unTow()
                this.popScene();
                return
            }
            $gameMessage._texts.push( `没有找到战车...`)
            this.popScene();
        }else{
            $gameMessage._texts.push( `没开车，无法牵引...`)
            this.popScene();
        }
    }
    //大地图 乘降
    Scene_UpDown.prototype.upDownActor = function(index){
        let actorIds = $gameParty._actors
        let actorId = actorIds[index]
        let ac = $gameActors._data[actorId]
        let first = index?$gamePlayer._followers._data[index-1]:$gamePlayer
        let p_x = first._realX
        let p_y = first._realY
        // 判断当前角色是否乘车
        if(!ac._driverTank){
            // 获取角色的坐标
            let tanks = $gameMap._tanks
            // 匹配当前大地图的战车 
            for(let i in tanks){
                if(!tanks[i]){
                    continue
                }
                //console.log(`人类ID：${ac._actorId}`, `坦克ID：${tanks[i]._tankId}`)
                if(`${p_x},${p_y}` == `${tanks[i]._realX},${tanks[i]._realY}`){
                    $gameMessage._texts.push(`${ac._name} 上车...` )
                    ac._driverTank = tanks[i]._tankId
                    ac._characterName = tanks[i]._characterName;
                    let gametank = $gameTanks.tank(tanks[i]._tankId)
                    gametank._driver = ac._actorId
                    //console.log('tank',gametank)
                    $gameMap.removeTank(tanks[i]._tankId)
                    ac.refresh()
                    $gamePlayer.refresh()
                    gametank.refresh()
                    playSound()
                    return
                }
            }
            $gameMessage._texts.push( `没有找到战车...`)
            
        }else{
            ac._characterName = $dataActors[actorId].characterName;
            let gametank = $gameTanks.tank(ac._driverTank)
            gametank._driver = null
            gametank._x = p_x
            gametank._y = p_y
            gametank._mapId = $gameMap._mapId
            gametank._direction = first._direction
            $gameMap.addTank(ac._driverTank, p_x,p_y)
            ac._driverTank = null
            //$gameMap.requestRefresh();
            $gameMessage._texts.push(`${ac._name} 下车...` )
            ac.refresh()
            $gamePlayer.refresh()
            playSound()
        }
        //判断是否还有人在乘车
        if(!$gameActors.checkDriver()){
            //取消牵引
            $gamePlayer._followers.unTow()
        }
        function playSound(){
            AudioManager.playSe({name:'0x37-Takeoff',pitch:100,volume:90,pan:0});
            //大地图才改变奏乐
            if($gameMap._tilesetId == 1 && $gameActors.checkDriver()){
                AudioManager.playBgm({name:'航道99',pitch:100,volume:90,pan:0});
            }else{
                AudioManager.playBgm($dataMap.bgm);
            }
        }

  
    }
    Scene_UpDown.prototype.commandWindowRect = function() {
        const ww = 400;
        const wh = this.calcWindowHeight(6, true) + 150;
        const wx = (Graphics.boxWidth - ww) / 2;
        const wy = (Graphics.boxHeight - wh) / 2;
        return new Rectangle(wx, wy, ww, wh);
    };


    //-----------------------------------------------------------------------------
    // 乘降窗口对象
    //
   
    function Window_UpDown() {
        this.initialize(...arguments);
    }

    Window_UpDown.prototype = Object.create(Window_Command.prototype);
    Window_UpDown.prototype.constructor = Window_UpDown;

    Window_UpDown.prototype.initialize = function(rect) {
        Window_Command.prototype.initialize.call(this, rect);
        this.openness = 0;
        this.open();
    };

    Window_UpDown.prototype.makeCommandList = function() {
        let names = $gameActors._data
        let party = $gameParty._actors
        //console.log(names,party)
        this.addCommand('全体', "updownPart",true);
        for(let i = 0 ;i < 3;i++){
            let _i = party[i]
            if(_i){
                this.addCommand(names[_i]._name + _i, "updownPart",true,i);
            }else{
                this.addCommand('', "cancel");
            }
            
        }
        this.addCommand('牵引', "tow");
        this.addCommand(TextManager.cancel, "cancel");
    };

    Window_UpDown.prototype.drawItem = function(index) {
        const rect = this.itemRectWithPadding(index);
        let actors = $gameActors._data
        let party = $gameParty._actors
        //log($gameParty)
        this.resetTextColor();
        const x =  rect.x
        const y =  rect.y + 10
        if(index == 0){
            this.drawText(`全部`, 160, y, 180);
            return
        }
        if(index == 4){
            this.drawText(`牵引`, 160, y, 280);
            return
        }
        if(index == 5){
            this.drawText(`取消`, 160, y, 180);
            return
        }
        let actor = actors[party[index-1]]
        if(actor){
            this.drawText(actor._name, x, y, 120,80);
            if(actor._driverTank){
                this.drawContents(actor._ActorPic,rect);
                let _rect = rect
                _rect.x = _rect.x + 180
                //console.log(actor._characterName)
                this.drawContents(actor._characterName,_rect);
            }else{
                this.drawContents(actor._characterName,rect);
            }
        }else{
            this.drawText('-', x, y, 120,80);
        }

    };
    Window_UpDown.prototype.drawContents = function(img,rect) {
        const bottom = rect.y + rect.height;
        this.drawCharacter(img, 0,rect.x + 120 , bottom - 8);
 
    };
    Window_UpDown.prototype.itemHeight = function() {
        return Window_Scrollable.prototype.itemHeight.call(this) + 30;
    };
    
    Window_UpDown.prototype.contentsHeight = function() {
        return this.innerHeight + this.itemHeight();
    };
    //
    Scene_MenuBase.prototype.commandWindowHeight = function() {
        return this.calcWindowHeight(2, true);
    };

    Scene_MenuBase.prototype.goldWindowHeight = function() {
        return this.calcWindowHeight(1, true);
    };
    Scene_Menu.prototype.goldWindowRect = function() {
        return new Rectangle(0, 0, 250, 70);
    };
    // Scene_Menu.prototype.goldWindowRect = function() {
    //     console.log( Window_Base.prototype)
    //     return Window_Base.prototype.changeTextColor('#ff5659');
    // };
    // 大菜单乘降
    Window_MenuCommand.prototype.addOriginalCommands = function () {
        this.addCommand("乘降", "updown", true);
    };
    Window_MenuCommand.prototype.makeCommandList = function() {
        this.addMainCommands(); //增加主菜单：物品、技能、装备、状态
        this.addOriginalCommands();//增加自定义的菜单
        this.addCommand("炮弹", "aim", true);
        this.addCommand("模式", "style", true);
        this.addGameEndCommand();//增加 游戏结束 菜单
    };
    Window_StatusBase.prototype.drawActorName = function(actor, x, y, width){
        width = width || 268;
        if(!actor){
            return
        }
        this.changeTextColor(ColorManager.hpColor(actor));
        this.contents.fontSize = 24
        this.drawText(actor.name(), x, y, width);
    }
    Window_StatusBase.prototype.drawActorLevel = function(actor, x, y) {
        // this.changeTextColor(ColorManager.systemColor());
        const _y = y + 20
        this.contents.fontSize = 14
        this.drawText(TextManager.levelA, x, _y, 30);
        this.resetTextColor();
        this.drawText(actor.level, x + 55, _y, 36, "right");
    };
    Window_StatusBase.prototype.drawActorFace = function(
        actor, x, y, width, height, dw ,dh
    ) {
        this.drawFace(actor.faceName(), actor.faceIndex(), x, y, width, height , dw ,dh);
    };
    Window_StatusBase.prototype.placeBasicGauges = function(actor, x, y) {
  
        this.placeGauge(actor,"hp", x, y + 70);
        //this.placeGauge(actor, "mp", x, y + this.gaugeLineHeight());
        // if ($dataSystem.optDisplayTp) {
        //     this.placeGauge(actor, "tp", x, y + this.gaugeLineHeight() * 2);
        // }
    };
    Sprite_Gauge.prototype.labelOutlineWidth = function() {
        return 0;
    };
    Sprite_Gauge.prototype.bitmapWidth = function() {
        return 158;
    };
    Window_StatusBase.prototype.drawActorClass = function(actor, x, y, width) {
        width = width || 168;
        this.resetTextColor();
        // this.drawText('猎人', x, y, width);
    };
    Sprite_Gauge.prototype.labelOutlineWidth = function() {
        return 0;
    };
    Sprite_Gauge.prototype.valueOutlineWidth = function() {
        return 0;
    };
    
    Window_StatusBase.prototype.placeGauge = function(actor, type, x, y) {
        const key = "actor%1-gauge-%2".format(actor.actorId(), type);
        const sprite = this.createInnerSprite(key, Sprite_Gauge);
        console.log(actor, type)
        sprite.setup(actor, type);
        sprite.move(x, y);
        sprite.bitmapWidth = function() {
            return 138;
        }
        if(actor._driver){
            sprite.label = function() {
                return 'SP';
            }
        }
        sprite.labelFontSize = function() {
            return 14;
        }
        sprite.valueFontSize = function() {
            return 14;
        };
        sprite.valueFontFace = function() {
            return $gameSystem.mainFontFace();
        };
        sprite.labelColor = function() {
            return '#ffffff'
        };
        sprite.gaugeColor1 = function(){
            return '#f8b800'
        }
        sprite.gaugeColor2 = function(){
            return '#c9ff55'
        }
        sprite.gaugeBackColor = function() {
            return '#9a1515'
        };
        sprite.show();
    };
    Window_MenuCommand.prototype.addMainCommands = function() {
        var enabled = this.areMainCommandsEnabled();
        if (this.needsCommand('item')) {
            this.addCommand(TextManager.item, 'item', enabled);//增加 物品 菜单
        }
        if (this.needsCommand('skill')) {
            this.addCommand(TextManager.skill, 'skill', enabled);//增加 技能 菜单
        }
        if (this.needsCommand('equip')) {
            this.addCommand(TextManager.equip, 'equip', enabled);//增加 装备 菜单
        }
        if (this.needsCommand('status')) {
            this.addCommand(TextManager.status, 'status', enabled);//增加 状态 菜单
        }
    };
    Scene_Menu.prototype.commandWindowRect = function() {
        const ww = Graphics.boxWidth;
        const wh = this.commandWindowHeight();
        const wx = 0;
        const wy = this.mainAreaHeight() / 1.25;
        return new Rectangle(wx + ww * 0.2, wy, ww * 0.6, wh);
    };
    Scene_Menu.prototype.createCommandWindow = function() {
        const rect = this.commandWindowRect();
        const commandWindow = new Window_MenuCommand(rect);
        commandWindow.setHandler("item", this.commandItem.bind(this));
        commandWindow.setHandler("skill", this.commandPersonal.bind(this));
        commandWindow.setHandler("equip", this.commandPersonal.bind(this));
        commandWindow.setHandler("status", this.commandPersonal.bind(this));
        commandWindow.setHandler("formation", this.commandFormation.bind(this));
        commandWindow.setHandler("options", this.commandOptions.bind(this));
        commandWindow.setHandler("save", this.commandSave.bind(this));
        commandWindow.setHandler("gameEnd", this.commandGameEnd.bind(this));
        commandWindow.setHandler("updown", this.commandUpdonw.bind(this));
        commandWindow.setHandler("cancel", this.popScene.bind(this));
        this.addWindow(commandWindow);
        this._commandWindow = commandWindow;
    };
    Scene_Menu.prototype.commandUpdonw = function(){
        SceneManager.goto(Scene_UpDown);
    }

    Scene_Menu.prototype.statusWindowRect = function() {
        const h1 = this.commandWindowHeight();
        const h2 = this.goldWindowHeight();
        const ww = Graphics.boxWidth;
        const wh = this.mainAreaHeight() - h1 - h2;
        const wx = 0;
        const wy = this.goldWindowHeight() + 15;
        return new Rectangle(wx, wy, ww, wh);
    };
    Scene_Map.prototype.createMenuButton = function() {
        //隐藏地图上的按钮
    }
    Scene_ItemBase.prototype.actorWindowRect = function() {
        const rect = Scene_Menu.prototype.statusWindowRect();
        rect.y = this.mainAreaBottom() - rect.height;
        return rect;
    };

    Window_MenuCommand.prototype.maxCols = function() {
        return 4;
    };

    Window_MenuCommand.prototype.numVisibleRows = function() {
        return 2;
    };
    Window_MenuStatus.prototype.maxItems = function() {
        return 8;
    };
    Window_MenuStatus.prototype.maxCols = function() {
        return 4;
    };

    Window_MenuStatus.prototype.numVisibleRows = function() {
        return 2;
    };
    Window_MenuStatus.prototype.drawItem = function(index) {
        this.drawPendingItemBackground(index);
        this.drawItemImage(index);
        this.drawItemStatus(index);
        //this.drawTanks(index);
    };
    Window_MenuStatus.prototype.drawItemImage = function(index) {
        const actor = this.actor(index);
        const rect = this.itemRectWithPadding(index);
        const w = rect.width 
        const h = rect.height
        const lineHeight = this.lineHeight();
        if(actor){
            this.changePaintOpacity(actor.isBattleMember());
            this.drawActorFace(actor, rect.x + 96, 0, 166, 166 ,60 ,60);
            this.changePaintOpacity(true);
        }
    };
    Window_MenuStatus.prototype.itemHeight = function() {
        return Math.floor(this.innerHeight / this.numVisibleRows());
    };
    Window_MenuStatus.prototype.drawItemStatus = function(index) {
        const actor = this.actor(index);
        const rect = this.itemRectWithPadding(index);
        const x = rect.x;
        const y = rect.y;
        const width = rect.width;
        const bottom = y + rect.height;
        const lineHeight = this.lineHeight();
        //console.log(index,'---->',actor)
        if(index < 4){
            if(actor){
                this.drawActorName(actor, x, y , width);
                this.drawText(actor.currentClass().name, x, y + 30,width );
                this.drawActorLevel(actor, x, y + lineHeight * 1);
                this.placeBasicGauges(actor, x, bottom - lineHeight * 3, width);

            }
        }else if(index < 7){
            const tank = this.tank(index - 4);
            if(!tank) return
            this.drawActorName(tank, x, y , width);
            this.placeBasicGauges(tank, x , y , width);
        }else if(index == 7){
            //是否有牵引战车
            let isTow = $gamePlayer._followers.isTow()
            if(isTow){
                let tank = $gameTanks.tank(isTow)
                if(!tank) return
                this.drawActorName(tank, x, y , width);
                this.placeBasicGauges(tank, x , y , width);
            }
        }
  
       
    };
    
    Window_MenuStatus.prototype.tank = function(index) {
        //console.log('===>',index)
        if(!$gameParty.members()[index])return null
        const tankId = $gameParty.members()[index]._driverTank
        if($gameParty.members()[index]._driverTank){
            return $gameTanks.tank(tankId)
        }else{
            return null;
        }
    };

    Window_MenuStatus.prototype.drawTanks = function(index){

        const tank = this.tank(index);
        const rect = this.itemRectWithPadding(index);
        const width = rect.width;
        const lineHeight = this.lineHeight();
        const x = rect.x;
        const y = rect.y;
        const bottom = y + rect.height;
        this.drawActorName(tank, x, y + lineHeight * 0, width);
    }

})();
